//
//  InvestSuccessViewController.m
//  Xspcf
//
//  Created by 赵欢 on 2018/1/24.
//  Copyright © 2018年 小小算盘. All rights reserved.
//

#import "InvestSuccessViewController.h"
#import "SunMyOrderVC.h"
@interface InvestSuccessViewController ()
// 投资成功
@property (weak, nonatomic) IBOutlet UILabel *successWord;
@property (weak, nonatomic) IBOutlet UIImageView *successLine;
@property (weak, nonatomic) IBOutlet UILabel *successDesc;
@property (weak, nonatomic) IBOutlet UIImageView *successBack;
@property (weak, nonatomic) IBOutlet UILabel *addDate;
@property (weak, nonatomic) IBOutlet UILabel *addTime;
@property (weak, nonatomic) IBOutlet UILabel *borrowName;
@property (weak, nonatomic) IBOutlet UILabel *investMoney;
@property (weak, nonatomic) IBOutlet UILabel *repayMoney;
@property (weak, nonatomic) IBOutlet UIButton *successButton;
- (IBAction)investRecordAction:(UIButton *)sender;

// 处理中或失败
@property (nonatomic, strong) UIView *backView;
@property (nonatomic, strong) UIImageView *signalImage;
@property (nonatomic, strong) UILabel *resultLabel;
@property (nonatomic, strong) UILabel *contentLabel;
@property (nonatomic, strong) UILabel *contentLabel2;
@property (nonatomic, strong) UILabel *kefulabel;
@property (nonatomic, strong) UIButton *finishButton;
@property (nonatomic, strong) UIButton *nextbutton;

@end

@implementation InvestSuccessViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self getInvestResult];

    self.backView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, SCREENWIDTH, Y(276))];
    self.backView.backgroundColor = WHITE;
    [self.view addSubview:self.backView];
    self.signalImage  = [[UIImageView alloc]initWithFrame:CGRectMake((SCREENWIDTH - 50)/2, Y(67), 50, 50)];
    [self.signalImage setImage:[UIImage imageNamed:@"loding_icon"]];
    [self.backView addSubview:self.signalImage];
    self.resultLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, Y(130), SCREENWIDTH, 33)];
    self.resultLabel.textAlignment = NSTextAlignmentCenter;
    self.resultLabel.textColor = [UIColor colorWithHexString:@"CECECE"];
    self.resultLabel.font = [UIFont fontWithName:@"PingFangSC-Medium" size:24];
    self.resultLabel.text = @"银行正在处理中";
    [self.backView addSubview:self.resultLabel];
    
    self.contentLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, Y(192), SCREENWIDTH, 33)];
    self.contentLabel.textAlignment = NSTextAlignmentCenter;
    self.contentLabel.textColor = [UIColor colorWithHexString:@"A2A2A2"];
    self.contentLabel.font = [UIFont fontWithName:@"PingFangSC-Regular" size:16];
    self.contentLabel.text = @"投资订单银行处理中，会有一定延迟。";
    [self.backView addSubview:self.contentLabel];
    self.contentLabel2 = [[UILabel alloc]initWithFrame:CGRectMake(0, Y(212), SCREENWIDTH, 33)];
    self.contentLabel2.textAlignment = NSTextAlignmentCenter;
    self.contentLabel2.textColor = [UIColor colorWithHexString:@"A2A2A2"];
    self.contentLabel2.font = [UIFont fontWithName:@"PingFangSC-Regular" size:16];
    self.contentLabel2.text = @"请稍后查询，如有疑问请联系客服！";
    [self.backView addSubview:self.contentLabel2];
    
    self.kefulabel = [[UILabel alloc]initWithFrame:CGRectMake(0, Y(292), SCREENWIDTH, 33)];
    self.kefulabel.textAlignment = NSTextAlignmentCenter;
    self.kefulabel.textColor = [UIColor colorWithHexString:@"D0021B"];
    self.kefulabel.font = [UIFont fontWithName:@"PingFangSC-Regular" size:12];
    self.kefulabel.text = @"客服热线:400-875-5858";
    [self.backView addSubview:self.kefulabel];
    
    self.nextbutton = [[UIButton alloc]initWithFrame:CGRectMake(X(46), Y(396),SCREENWIDTH - X(92), X(44))];
    [self.view addSubview:self.nextbutton];
    [self.nextbutton setBackgroundImage:[UIImage imageNamed:@"sign_button"] forState:UIControlStateNormal];
    [self.nextbutton setTitle:@"查看投资记录" forState:UIControlStateNormal];
    [self.nextbutton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [self.nextbutton.titleLabel setFont:[UIFont fontWithName:@"PingFangSC-Regular" size:18]];
//    self.nextbutton.titleLabel.font = [UIFont fontWithName:@"PingFangSC-Regular" size:18];
    [self.nextbutton addTarget:self action:@selector(nextAction) forControlEvents:UIControlEventTouchUpInside];
    // Do any additional setup after loading the view from its nib.
}
- (void)nextAction{
    [self dismissViewControllerAnimated:YES completion:nil];
}
- (void)getInvestResult{
    NSString *url = [NSString stringWithFormat:@"%@%@",KURL,API_TENDERRESULT];
    NSMutableDictionary *dic = [NSMutableDictionary dictionary];
    [dic setObject:self.borrowId forKey:@"borrowId"];
    if (self.tenderId != nil) {
        [dic setObject:self.tenderId forKey:@"tenderId"];
    }
    if (self.rechargeId != nil) {
        [dic setObject:self.rechargeId forKey:@"rechargeId"];
    }

    [HttpRequest postURL:url withParam:dic success:^(id requestDic, NSString *msg) {
        NSString *remark = [requestDic objectForKey:@"remark"];
        if ([remark containsString:@"成功"]) {
            [self hiddenSuccess:NO];
            NSString * addTime = [Sun timestamp:[[requestDic objectForKey:@"addTime"] intValue] ToTime:@"YY/MM/dd HH:mm"];
            self.addDate.text = [addTime substringToIndex:addTime.length - 6];
            self.addTime.text = [addTime substringFromIndex:addTime.length - 5];
            self.borrowName.text = [NSString stringWithFormat:@"成功投资%@",[requestDic objectForKey:@"name"]];
            self.investMoney.text = [NSString stringWithFormat:@"%@元",[requestDic objectForKey:@"account"]];
            self.repayMoney.text = [NSString stringWithFormat:@"%@元",[requestDic objectForKey:@"repayAccount"]];
        }else if ([remark containsString:@"处理"]) {
            [self hiddenSuccess:YES];

        }else if ([remark containsString:@"失败"]) {
            [self hiddenSuccess:YES];
            [self.signalImage setImage:[UIImage imageNamed:@"lose_icon"]];
            self.resultLabel.text = @"投资失败";
            self.contentLabel.text = @"非常抱歉，您的订单银行处理失败 ";
            self.contentLabel2.text = @"请您稍后重新投资，给您带来不便 敬请谅解！";
            
        }
    } failure:^(NSString *errorInfo, int code) {
        [[Sun shareSun]showMsg:errorInfo];
    }];
}
- (void)hiddenSuccess:(BOOL)isHidden {
    self.successWord.hidden = isHidden;
    self.successLine.hidden = isHidden;
    self.successDesc.hidden = isHidden;
    self.successBack.hidden = isHidden;
    self.addDate.hidden = isHidden;
    self.addTime.hidden = isHidden;
    self.borrowName.hidden = isHidden;
    self.investMoney.hidden = isHidden;
    self.repayMoney.hidden = isHidden;
    self.successButton.hidden = isHidden;
    self.backView.hidden = !isHidden;
    self.nextbutton.hidden = !isHidden;
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (IBAction)investRecordAction:(UIButton *)sender {
    [self dismissViewControllerAnimated:YES completion:^{
        
    }];
}
@end
